package com.zghhost.stockweb.service.impl;

import com.zghhost.stockweb.constans.KLineConstans;
import com.zghhost.stockweb.pojo.StockDayInfo;
import com.zghhost.stockweb.util.HttpUtil;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * @author guohua.zhang@zhaopin.com.cn
 * @Date 2019/5/18 14:00
 */
@Service
public class KLineService {

    public List<StockDayInfo> getKLine(String code, String start, String end)throws Exception{
        //Echart 数据格式 日期 开盘 收盘 最高 最低 交易量
        //Object[][] sdata = new Object[][]{{"2004-01-02",10452.74,10409.85,10367.41,10554.96,168890000}};
        List<StockDayInfo> data = new ArrayList<>();

        InputStream inputStream = HttpUtil.doHttpGetAsStream(String.format(KLineConstans.K_LINE_URL,code,start,end));
        BufferedReader br = new BufferedReader(new InputStreamReader(inputStream,"gb2312"));
        try {
            //去除第一行
            br.readLine();

            String line;
            while ((line = br.readLine()) != null){
                String[] props = line.split(",");

                try{
                    //日期,股票代码,名称, 收盘价,最高价,最低价,开盘价 ,前收盘,涨跌额,涨跌幅, 换手率,成交量,成交金额, 总市值,流通市值,成交笔数
                    StockDayInfo prop = new StockDayInfo();

                    prop.setRiqi(props[0]);
                    prop.setDaima(props[1]);
                    prop.setMingcheng(props[2]);

                    prop.setShowpan(Double.valueOf(props[3]));
                    prop.setZuigao(Double.valueOf(props[4]));
                    prop.setZuidi(Double.valueOf(props[5]));
                    prop.setKaipan(Double.valueOf(props[6]));

//                prop.setQianshoupan(Double.valueOf(props[7]));
                    prop.setZhangdiee(Double.valueOf(props[8]));
                    prop.setZhangdiefu(Double.valueOf(props[9]));

//                prop.setHuanshoulv(Double.valueOf(props[10]));
                    prop.setChengjiaoliang(Double.valueOf(props[11]));
                    prop.setChengjiaoe(Double.valueOf(props[12]));

//                prop.setZongshizhi(Double.valueOf(props[13]));
//                prop.setLiutongshizhi(Double.valueOf(props[14]));
//                    prop.setChengjiaobishu(Double.valueOf(props[15]));

                    data.add(prop);
                }catch (Exception e){

                }
            }
        }catch (Exception e){
            throw e;
        }finally {
            inputStream.close();
            br.close();
        }
        return data;
    }
}
